<?xml version="1.0" encoding="UTF-8"?>
<#assign gentime = .now>
<!--
      ${name}模块对应的数据库访问配置文件
	  since ${gentime?string['yyyy-MM-dd HH:mm']}
	  author autogen
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
   <#assign field="">	
	 <#list list as col >
		<#if col_has_next>
		  <#assign field="${field}${col.column_name} ${col.column_alias},">	 
		<#else>
		  <#assign field="${field}${col.column_name} ${col.column_alias}">	 
		</#if>
  </#list>
  
<#assign listField="">	
	 <#list list?sort_by("list_show") as col >
		<#if col.column_key=="PRI"&& col.list_show=="N">
			 <#assign listField="${listField}${col.column_name} ${col.column_alias},">	 
		</#if>
		<#if col.list_show=="Y">
			<#if col_has_next >
			  <#assign listField="${listField}${col.column_name} ${col.column_alias},">	 
			<#else>
			  <#assign listField="${listField}${col.column_name} ${col.column_alias}">	 
			</#if>
		</#if>
  </#list>
<mapper namespace="${daoPkg}.${name?cap_first}Mapper">  
	<resultMap id="list${name?cap_first}Map" type="${name?cap_first}Entity">
	 <#list list?sort_by("list_show") as col >		 
		<#if col.list_show=="Y">
			<#if col.column_key=="PRI">
				<id property="${col.column_alias}" column="${col.column_name}" />
			<#else>
				<result property="${col.column_alias}" column="${col.column_name}" />
			</#if> 
		<#else>
			<#if col.column_key=="PRI">
				<id property="${col.column_alias}" column="${col.column_name}" />			 
			</#if> 
		</#if>
  </#list>
	</resultMap>
 
	<select id="get${name?cap_first}ByID" parameterType="${pkCol.data_type}" resultType="${name?cap_first}Entity">
		 select ${field} from ${table} where ${pkCol.column_name} = ${r"#{"}${pkCol.column_alias}${r"}"} 
	</select> 
 
<#if showStyle=="TREE"||showStyle=="TREE_TABLE">
	<select id="get${name?cap_first}Childrens" parameterType="${pkCol.data_type}" resultType="${name?cap_first}Entity"> 
		 select ${field} from ${table} where p${pkCol.column_name} = ${r"#{"}${pkCol.column_alias}${r"}"} 
	</select> 
</#if>

	<select id="get${name?cap_first}List" parameterType="${name?cap_first}Query" resultMap="list${name?cap_first}Map">
	   select ${listField} from ${table} 
		 <trim prefix="WHERE" prefixOverrides="AND |OR ">		 
		 <#list list?sort_by("list_show") as col >
			<#if col.list_query=="Y">
				 <#if col.data_type=="date"||col.data_type=="datetime">
					<if test="${col.column_alias}_from != null ">
							AND ${col.column_name} &gt;= ${r"#{"}${col.column_alias}_from${r"}"} 
					</if>
					<if test="${col.column_alias}_to != null ">
							AND ${col.column_name} &lt;= ${r"#{"}${col.column_alias}_to${r"}"} 
					</if>
				 <#elseif col.data_type=="String">
					<if test="${col.column_alias} != null and ${col.column_alias}!='' ">
							AND ${col.column_name} like ${r"#{"}${col.column_alias}${r"}"} 
					</if>
				 <#else>
							AND ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} 
				 </#if>
			</#if>
	  </#list>
		</trim>
		<choose>
			<when test="sort!=null and sort!='' "> order by ${r"#{"}${"sort"}${r"}"}  </when>			
			<otherwise> order by ${pkCol.column_name} desc </otherwise>
		</choose>
		<if test="page > 0 ">
			 limit ${r"#{"}${"start"}${r"}"},${r"#{"}${"rows"}${r"}"}
		</if>
	</select>
	
	<select id="get${name?cap_first}All" parameterType="${name?cap_first}Query" resultMap="list${name?cap_first}Map">
		  select ${listField} from ${table} 
		 <trim prefix="WHERE" prefixOverrides="AND |OR ">		 
		 <#list list?sort_by("list_show") as col >
			<#if col.list_query=="Y">
				 <#if col.data_type=="date"||col.data_type=="datetime">
					<if test="${col.column_alias}_from != null ">
							AND ${col.column_name} &gt;= ${r"#{"}${col.column_alias}_from${r"}"} 
					</if>
					<if test="${col.column_alias}_to != null ">
							AND ${col.column_name} &lt;= ${r"#{"}${col.column_alias}_to${r"}"} 
					</if>
				 <#elseif col.data_type=="String">
					<if test="${col.column_alias} != null and ${col.column_alias}!='' ">
							AND ${col.column_name} like ${r"#{"}${col.column_alias}${r"}"}  
					</if>
				 <#else>
							AND ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"}  
				 </#if>
			</#if>
	  </#list>
		</trim>
	</select>
	
	<select id="get${name?cap_first}Count" parameterType="${name?cap_first}Query" resultType="int">
	 select count(0) from ${table} 
		<trim prefix="WHERE" prefixOverrides="AND|OR">
		 <#list list?sort_by("list_show") as col >
			<#if col.list_query=="Y">
				 <#if col.data_type=="date"||col.data_type=="datetime">
					<if test="${col.column_alias}_from != null ">
							AND ${col.column_name} &gt;= ${r"#{"}${col.column_alias}_from${r"}"}  
					</if>
					<if test="${col.column_alias}_to != null ">
							AND ${col.column_name} &lt;= ${r"#{"}${col.column_alias}_to${r"}"} 
					</if>
				 <#elseif col.data_type=="String">
					<if test="${col.column_alias} != null and ${col.column_alias}!='' ">
							AND ${col.column_name} like ${r"#{"}${col.column_alias}${r"}"}  
					</if>
				 <#else>
							AND ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} 
				 </#if>
			</#if>
		</#list>
		</trim>
	</select>
	
	<insert id="save${name?cap_first}" parameterType="${name?cap_first}Entity"	<#if pkCol.column_key=="PRI"&& pkCol.extra="auto_increment"> keyProperty="${pkCol.column_alias}" keyColumn="${pkCol.column_name}" useGeneratedKeys="true"  </#if> > 
	<#assign field="">
	<#list list?sort_by("new_show") as col >
		<#if col.column_key=="PRI" && col.extra=="auto_increment">		
		<#else>
			<#if col.new_show=="Y">
			<#if col_has_next >
			  <#assign field="${field}${col.column_name} ,">			  
			<#else>
			  <#assign field="${field}${col.column_name} ">	 		
			</#if> 
			</#if>
		</#if>
	</#list>
	  insert into ${table} (${field}) 
	  values(
<#list list?sort_by("new_show") as col ><#if col.column_key=="PRI" && col.extra=="auto_increment"><#else><#if col.new_show=="Y"><#if col_has_next > ${r"#{"}${col.column_name}${r"}"} ,<#else> ${r"#{"}${col.column_name}${r"}"}</#if></#if> </#if></#list>
            )
	</insert>
	<update id="update${name?cap_first}" parameterType="${name?cap_first}Entity">
		update ${table}
		<trim prefix="SET" suffixOverrides=",">
	<#list list?sort_by("update_show") as col >
		<#if col.update_show=="Y">
		<#if col_has_next>
		<#if col.data_type=="date"||col.data_type=="datetime">
			 <if test="${col.column_alias} != null  ">				
			  ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} , 				 
			 </if> 
		<#elseif col.data_type=="String">
			<if test="${col.column_alias} != null and ${col.column_alias}!='' ">
				 ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"}  ,	
			</if>
		<#else>
			  ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} ,
		</#if>
		<#else>
			<#if col.data_type=="date"||col.data_type=="datetime">
			 <if test="${col.column_alias} != null  ">				
			  ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} 				 
			 </if> 
		<#elseif col.data_type=="String">
			<if test="${col.column_alias} != null and ${col.column_alias}!='' ">
				 ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} 	
			</if>
		<#else>
			  ${col.column_name} = ${r"#{"}${col.column_alias}${r"}"} 
		</#if>
		
		</#if>
		</#if>
	</#list>
		</trim>		
		where ${pkCol.column_name}=${r"#{"}${pkCol.column_alias}${r"}"} 
	</update>
	
	<delete id="delete${name?cap_first}" parameterType="${pkCol.data_type}">
		delete from ${table}
		where ${pkCol.column_alias} in 
		 <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
			${r"#{"}${"item"}${r"}"}
		</foreach>
	</delete>
</mapper>